<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="${comment}" prop="trainingSchoolId">
        <el-input
          v-model="queryParams.trainingSchoolId"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsSn">
        <el-input
          v-model="queryParams.goodsSn"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否必选" prop="required">
        <el-input
          v-model="queryParams.required"
          placeholder="请输入是否必选"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学期" prop="semesterId">
        <el-input
          v-model="queryParams.semesterId"
          placeholder="请输入学期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品所属类目ID" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入商品所属类目ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:goods:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" border>
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="${comment}" align="center" prop="trainingSchoolId" />
      <el-table-column label="商品编号" align="center" prop="goodsSn" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="是否必选" align="center" prop="required" />
      <el-table-column label="学期" align="center" prop="semesterId" />
      <el-table-column label="${comment}" align="center" prop="schoolType" />
      <el-table-column label="${comment}" align="center" prop="gradeType" />
      <el-table-column label="商品所属类目ID" align="center" prop="categoryId" />
      <el-table-column label="出版社" align="center" prop="publisher" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="商品简介" align="center" prop="brief" />
      <el-table-column label="是否上架" align="center" prop="isOnSale" />
      <el-table-column label="${comment}" align="center" prop="sortOrder" />
      <el-table-column label="商品页面商品图片" align="center" prop="picUrl" />
      <el-table-column label="专柜价格" align="center" prop="counterPrice" />
      <el-table-column label="零售价格" align="center" prop="retailPrice" />
      <el-table-column label="商品详细介绍，是富文本格式" align="center" prop="detail" />
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="逻辑删除" align="center" prop="deleted" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:goods:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改商品基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="trainingSchoolId">
          <el-input v-model="form.trainingSchoolId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="商品编号" prop="goodsSn">
          <el-input v-model="form.goodsSn" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="是否必选" prop="required">
          <el-input v-model="form.required" placeholder="请输入是否必选" />
        </el-form-item>
        <el-form-item label="学期" prop="semesterId">
          <el-input v-model="form.semesterId" placeholder="请输入学期" />
        </el-form-item>
        <el-form-item label="商品所属类目ID" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入商品所属类目ID" />
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社" />
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="商品简介" prop="brief">
          <el-input v-model="form.brief" placeholder="请输入商品简介" />
        </el-form-item>
        <el-form-item label="是否上架" prop="isOnSale">
          <el-input v-model="form.isOnSale" placeholder="请输入是否上架" />
        </el-form-item>
        <el-form-item label="${comment}" prop="sortOrder">
          <el-input v-model="form.sortOrder" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="商品页面商品图片" prop="picUrl">
          <el-input v-model="form.picUrl" placeholder="请输入商品页面商品图片" />
        </el-form-item>
        <el-form-item label="专柜价格" prop="counterPrice">
          <el-input v-model="form.counterPrice" placeholder="请输入专柜价格" />
        </el-form-item>
        <el-form-item label="零售价格" prop="retailPrice">
          <el-input v-model="form.retailPrice" placeholder="请输入零售价格" />
        </el-form-item>
        <el-form-item label="商品详细介绍，是富文本格式" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods } from "@/api/jiaofu/goods";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品基本信息表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainingSchoolId: null,
        goodsSn: null,
        name: null,
        required: null,
        semesterId: null,
        schoolType: null,
        gradeType: null,
        categoryId: null,
        publisher: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsSn: [
          { required: true, message: "商品编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品基本信息列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainingSchoolId: null,
        goodsSn: null,
        name: null,
        required: null,
        semesterId: null,
        schoolType: null,
        gradeType: null,
        categoryId: null,
        publisher: null,
        author: null,
        brief: null,
        isOnSale: null,
        sortOrder: null,
        picUrl: null,
        counterPrice: null,
        retailPrice: null,
        detail: null,
        addTime: null,
        updateTime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品基本信息";
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
